package com.example.springboot.handler;

import com.example.springboot.document.ResultBo;
import com.example.springboot.exception.BaseException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Description: RestController全局异常处理类
 * 有两个@ExceptionHandler处理同一个异常的方法，@ControllerAdvice则优先于@RestControllerAdvice
 * @Author wenlinyu
 * @Date: 2020/12/16 15:56
 * @Version 1.0
 */
@RestControllerAdvice(annotations = RestController.class)
public class RestControllerExceptionHandler {

    /**
     * 当发生BaseException类异常时，调用此方法
     * @param e
     * @return
     */
    @ExceptionHandler({BaseException.class})
    public ResultBo baseExceptionHandler(BaseException e) {
        return new ResultBo(e);
    }

    /**
     * 处理Exception类的异常
     * @param e
     * @return
     */
    @ExceptionHandler({Exception.class})
    public ResultBo excetionHandler(Exception e) {
        return new ResultBo(e);
    }

}
